package com.truyen.service.impl;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.List;

import net.sf.ehcache.CacheManager;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.googlecode.ehcache.annotations.Cacheable;
import com.truyen.persistence.dao.ChapterTamDao;
import com.truyen.persistence.entity.ChapterTam;
import com.truyen.persistence.entity.LoaiLog;
import com.truyen.persistence.entity.TruyenKiemHiep;
import com.truyen.persistence.entity.TruyenMa;
import com.truyen.persistence.entity.TruyenTinhCam;
import com.truyen.persistence.entity.TruyenTranh;
import com.truyen.persistence.entity.TruyenTrinhTham;
import com.truyen.service.ChapterTamService;
import com.truyen.service.LoaiLogService;
import com.truyen.service.LogOtherService;
import com.truyen.utils.PagingInfo;


@Service
public class ChapterTamServiceImpl implements ChapterTamService {
	
	static Logger log=Logger.getLogger(JsoupServiceImpl.class);
	
	@Autowired
	private ChapterTamDao chapterTamDao;
	
	@Autowired
	public CacheManager cacheManager;
	
	@Autowired
	private LogOtherService logOtherService;
	
	@Autowired
	private LoaiLogService loaiLogService;
	
	// ham cap nhat number cua chapter
	public float getChapterNumber(String nameChapter) {
		LoaiLog loaiLog = loaiLogService.findByProperty("alias", "doi chapter number");
		try {
			// xu ly cat ten truyen den dau :
			int endIndex = nameChapter.lastIndexOf(":");
			if (endIndex == -1) {
				endIndex = nameChapter.length();
			}
			int beginIndex = endIndex - 5;
			if (beginIndex < 0) {
				beginIndex = endIndex - 3;
			}
			String str = nameChapter.substring(beginIndex, endIndex);
			// xu ly cat ten truyen den dau khoang cach sau cung
			endIndex = str.length();
			beginIndex = str.indexOf(" ");
			int khoangTrang = endIndex - beginIndex;
			if (beginIndex > 0 && khoangTrang > 1) {
				str = str.substring(beginIndex, endIndex);
			}
			DecimalFormatSymbols symbols = new DecimalFormatSymbols();
			symbols.setDecimalSeparator('.');
			DecimalFormat format = new DecimalFormat("0.#");
			format.setDecimalFormatSymbols(symbols);
			float chapterNumber = format.parse(str.trim()).floatValue();
			return chapterNumber;
		} catch (Exception e) {
			logOtherService.writeLog("ERROR - Cập nhật chaterNumber của chapter tạm", "Có lỗi xãy ra khi cập nhật chapternumber của chapter tạm "+nameChapter, loaiLog, true);
			return 0;
		}

	}
		
	@Override
	public void updateLstChapterNumberBlogTruyen() {
		LoaiLog loaiLog = loaiLogService.findByProperty("alias", "doi chapter number");
		List<ChapterTam> lstAll = getLstFromSourceBy(1);
		int size = lstAll.size();
		for (int i = 0; i < size; i++) {
			ChapterTam chapter = lstAll.get(i);
			String nameChapter = chapter.getName();
			try {
				float newChapterNumber = getChapterNumber(nameChapter);
				if (newChapterNumber > 0) {
					chapter.setChapterNumber(newChapterNumber);
					chapterTamDao.merge(chapter);
				}
			}catch (Exception e) {
				logOtherService.writeLog("ERROR - Cập nhật chaterNumber của danh sách chapter tạm", "Có lỗi xãy ra", loaiLog, true);
			}//ket thuc try catch
			logOtherService.writeLog("OK - Cập nhật chaterNumber của danh sách chapter tạm","Đã cập nhật xong", loaiLog, false);
		}//ket thuc for (int i = 0; i < size; i++) {
	}//ket thuc ham

	@Override
	public void save(ChapterTam entity) {
		chapterTamDao.save(entity);
	}

	@Override
	public ChapterTam findById(Long id) {
		return chapterTamDao.findById(id);
	}

	@Override
	public List<ChapterTam> getAll() {
		return chapterTamDao.findAll();
	}

	@Override
	public ChapterTam findByProperty(String column, Object value) {
		List<ChapterTam> lst = chapterTamDao.findByQuery(column, value);
		return (lst.size() > 0)?lst.get(0):null;
	}

	@Override
	public ChapterTam findAliasvTruyen(TruyenTranh truyen, String alias) {
		return chapterTamDao.findAliasvTruyen(truyen,alias);
	}

	@Override
	public List<ChapterTam> getLstFromSourceBy(int sourceBy) {
		return chapterTamDao.getLstFromSourceBy(sourceBy);
	}

	@Override
	@Transactional
	public void delete(long id) {
		ChapterTam entity = findById(id);
		if(entity != null){
			chapterTamDao.delete(entity);
		}
		
	}

	@Override
	@Cacheable(cacheName="chapterTamCacheDataController")
	public List<ChapterTam> getLstChapterTamByTruyen(TruyenTranh truyen, String kwSearch, String sort, String sortBy, PagingInfo pInfo) {
		// TODO Auto-generated method stub
		return chapterTamDao.getLstChapterTamByTruyen(truyen,kwSearch,sort,sortBy,pInfo);
	}

	@Override
	@Cacheable(cacheName="chapterTamAllCacheDataController")
	public List<ChapterTam> getLstAllChapterTam(String kwSearch, String sort, String sortBy, PagingInfo pInfo) {
		return chapterTamDao.getLstAllChapterTam(kwSearch,sort,sortBy,pInfo);
	}

	@Override
	public List<ChapterTam> getByTruyen(TruyenTranh entity) {
		return chapterTamDao.getByTruyen(entity);
	}

	@Override
	public void updateChapterNumber(Long id, float chapterNumberChange) {
		try{
			ChapterTam chapterTam = findById(id);
			if(chapterTam != null){
				chapterTam.setChapterNumber(chapterNumberChange);
				chapterTamDao.merge(chapterTam);
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
		
	}

	@Transactional
	@Override
	public void updateLinkSource(Long id, String linkSource) {
		try{
			ChapterTam chapter = findById(id);
			if(chapter != null){
				chapter.setLinkSource(linkSource);
				chapterTamDao.merge(chapter);
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
		
	}

	@Override
	public List<ChapterTam> getByTruyen(TruyenTrinhTham entity) {
		return chapterTamDao.getByTruyen(entity);
	}

	@Override
	public List<ChapterTam> getByTruyen(TruyenTinhCam entity) {
		return chapterTamDao.getByTruyen(entity);
	}

	@Override
	public List<ChapterTam> getByTruyen(TruyenKiemHiep entity) {
		return chapterTamDao.getByTruyen(entity);
	}
	
	@Override
	public List<ChapterTam> getByTruyen(TruyenMa entity) {
		return chapterTamDao.getByTruyen(entity);
	}

	@Override
	public ChapterTam findAliasvTruyen(TruyenKiemHiep truyen, String alias) {
		return chapterTamDao.findAliasvTruyen(truyen,alias);
	}

	@Override
	public ChapterTam findAliasvTruyen(TruyenTrinhTham truyen, String alias) {
		return chapterTamDao.findAliasvTruyen(truyen,alias);
	}

	@Override
	public ChapterTam findAliasvTruyen(TruyenTinhCam truyen, String alias) {
		return chapterTamDao.findAliasvTruyen(truyen,alias);
	}

	@Override
	public List<ChapterTam> getLstFromSourceByVTruyenNotNull(int sourceBy,
			String truyenNotNull) {
		return chapterTamDao.getLstFromSourceByVTruyenNotNull(sourceBy,truyenNotNull);
	}

	@Override
	public List<ChapterTam> getLstChapterTamByTruyen(TruyenKiemHiep truyen,
			String kwSearch, String sort, String sortBy, PagingInfo pInfo) {
		return chapterTamDao.getLstChapterTamByTruyen(truyen,kwSearch,sort,sortBy,pInfo);
	}

	@Override
	public List<ChapterTam> getLstChapterTamByTruyen(TruyenTinhCam truyen,
			String kwSearch, String sort, String sortBy, PagingInfo pInfo) {
		return chapterTamDao.getLstChapterTamByTruyen(truyen,kwSearch,sort,sortBy,pInfo);
	}

	@Override
	public List<ChapterTam> getLstChapterTamByTruyen(TruyenTrinhTham truyen,
			String kwSearch, String sort, String sortBy, PagingInfo pInfo) {
		return chapterTamDao.getLstChapterTamByTruyen(truyen,kwSearch,sort,sortBy,pInfo);
	}

	@Override
	public ChapterTam findAliasvTruyen(TruyenMa truyen,
			String alias) {
		return chapterTamDao.findAliasvTruyen(truyen,alias);
	}

	@Override
	public List<ChapterTam> getLstChapterTamByTruyen(TruyenMa truyen,
			String kwSearch, String sort, String sortBy, PagingInfo pInfo) {
		return chapterTamDao.getLstChapterTamByTruyen(truyen,kwSearch,sort,sortBy,pInfo);
	}

}
